#include "LineEditCompleteListShell.h"

XX_KEY_C(LineEditCompleteListShell,kCompleteItem,"completeItem")

LineEditCompleteListShell::LineEditCompleteListShell(QObject *parent)
    : LineEditShell (parent){
    _indicatorButton.setObjectName("pushButton_indicator");
    _indicatorButton.setCheckable(true);
    _completeList.setObjectName("listWidget_complete");
    _completeList.hide();
    _completeList.setFixedHeight(150);
    connect(&_completeList,&QListWidget::itemDoubleClicked,this,&LineEditCompleteListShell::listWidget_completeList_onItemDblClicked);
}

void LineEditCompleteListShell::config(const QVariant &param){
    LineEditShell::config(param);
    if(param.canConvert(QMetaType::QStringList)){
        configCompleteItem(param.toStringList());
    }
    else if(param.type() == QVariant::Map){
        QVariantMap map = param.toMap();
        if(map.contains(kCompleteItem)){
            configCompleteItem(map[kCompleteItem].toStringList());
        }
    }
    else{

    }
}
void LineEditCompleteListShell::configCompleteItem(const QStringList &item){
    _completeItem = item;
    _completeList.clear();
    _completeList.addItems(item);
}

bool LineEditCompleteListShell::eventFilter(QObject *w, QEvent *e){
    if(target() == w){
        if(QEvent::FocusIn == e->type()){
            auto *lineEdit = qobject_cast<QLineEdit*>(target());
            complete(lineEdit->text());
            popup();
        }
        else if(QEvent::FocusOut == e->type()){
            popdown();
        }
        else{

        }
    }
    return LineEditShell::eventFilter(w,e);
}
void LineEditCompleteListShell::installTarget(const QString &name, QObject *target){
    QWidget *w = qobject_cast<QWidget*>(target);
    if(nullptr == w){
        return;
    }
    _completeList.setParent(w);
    _indicatorButton.setParent(w);

    _layoutSet<<QSharedPointer<XXlayoutConstraint*>(XXlayoutConstraint::constraint(&_indicatorButton,XXlayoutConstraint::Right,w,XXlayoutConstraint::Right,0));
    _layoutSet<<QSharedPointer<XXlayoutConstraint*>(XXlayoutConstraint::constraint(&_indicatorButton,XXlayoutConstraint::CenterY,w,XXlayoutConstraint::CenterY,0));
    _layoutSet<<QSharedPointer<XXlayoutConstraint*>(XXlayoutConstraint::constraint(&_completeList,XXlayoutConstraint::Left,w,XXlayoutConstraint::Left,0));
    _layoutSet<<QSharedPointer<XXlayoutConstraint*>(XXlayoutConstraint::constraint(&_completeList,XXlayoutConstraint::Width,w,XXlayoutConstraint::Width,0));
    w->installEventFilter(this);
}
void LineEditCompleteListShell::uninstallTarget(const QString &name, QObject *target){
    _layoutSet.clear();
    target->removeEventFilter(this);
}
void LineEditCompleteListShell::textDidChanged(QString previous, QString current){
    complete(current);
}

void LineEditCompleteListShell::active(bool active){
    _indicatorButton.setHidden(!active);
    _indicatorButton.setChecked(active);
}
void LineEditCompleteListShell::popup(){
    active(true);
    _completeList.show();
}
void LineEditCompleteListShell::popdown(){
    active(false);
    _completeList.hide();
}
void LineEditCompleteListShell::complete(const QString &begin){
    QStringList c;
    foreach(auto item,_completeItem){
        if(item.startsWith(begin)){
            c<<item;
        }
    }
    _completeList.clear();
    _completeList.addItems(c);
}
void LineEditCompleteListShell::listWidget_completeList_onItemDblClicked(QListWidgetItem *item){
    QLineEdit *lineEdit = ttarget<QLineEdit>();
    lineEdit->setText(item->text());
}
